﻿using DotnetSpider.Core;
using DotnetSpider.Core.Processor;
using System;
using System.Collections.Generic;

using AngleSharp.Html.Parser;
using SpiderPlatform.Entity;

namespace SpiderPlatform.Processor
{
    public class KouhighProcessor : BasePageProcessor
    {
        private static HtmlParser htmlParser = new HtmlParser();
        protected override void Handle(Page page)
        {
            List<KouhighEntity> list = new List<KouhighEntity>();
            var dom = htmlParser.ParseDocument(page.Content);
            var HtmlList = dom.QuerySelectorAll("ul.border-lt.goods-list li");
            foreach (var item in HtmlList)
            {
                KouhighEntity entity = new KouhighEntity();
                entity.images = item.QuerySelector("dt.bg-ff img").GetAttribute("data-original");
                entity.name = item.QuerySelector("dd div.ellipsis-2.sell-info").TextContent.Trim();
                entity.price = item.QuerySelector("dd div.price-info span.color-primary").TextContent;
                entity.id = item.GetAttribute("id");
                entity.link = page.Url;
                var soldOut = item.QuerySelector("div.media-left div.product-sold-out");
                if (soldOut != null)
                {
                    entity.is_instock = 1;
                }
                list.Add(entity);
            }
            page.AddResultItem("list", list);
            var pageInfo = dom.QuerySelectorAll("ul.el-pager li.number");
            foreach (var item in pageInfo)
            {
                page.AddTargetRequest("");
            }
        }
    }    
  
}
